/* Funzione che aggiusta un'area */

aggiustaArea(A,B):-
	sitiInteresseIn(A,Siti),
	length(Siti,N),
	N>1,
	area(A,[Ax,Ay,Ad]),
	assert(insiemeSiti(Siti)),
	bagMin(xSitiInArea,MinX),
	bagMin(ySitiInArea,MinY),
	bagMax(xSitiInArea,MaxX),
	bagMax(ySitiInArea,MaxY),
	retractall(insiemeSiti(_)),
	Bd2 is -(MaxX,MinX),
	Bd3 is -(MaxY,MinY),
        max(Bd2,Bd3,Bd1),
	Bx1 is +(MinX,-(Bd1,+(Ax,Ad))),
	By1 is +(MinY,-(Bd1,+(Ay,Ad))),
	(Bx1>0->Bx2 is -(MinX,Bx1);Bx2 is MinX),
	(By1>0->By2 is -(MinY,By1);By2 is MinY),
	Bx is -(Bx2,0.0005),
	By is -(By2,0.0005),
	Bd is +(Bd1,0.001),
	tx(A,Tx),
	percorso(A,P),
	((area(B,[Bx,By,Bd]),!);
	 (idArea(B),assert(area(B,[Bx,By,Bd])),assert(tx(B,Tx)),assert(percorso(B,P)))
	),
	rimuoviArea(A),
	[aggregs].


aggiustaArea(A,B):-
	sitiInteresseIn(A,Siti),
	Siti=[S],
	tsiti(S,_,_,_,_,Ax,Ay,_),
	Bx is -(Ax,0.0005),
	By is -(Ay,0.0005),
	Bd is 0.004,
	tx(A,Tx),
	percorso(A,P),
	((area(B,[Bx,By,Bd]),!);
	 (idArea(B),assert(area(B,[Bx,By,Bd])),assert(tx(B,Tx)),assert(percorso(B,P)))
	),
	rimuoviArea(A).
	

:- hilog xSitiInArea.
xSitiInArea(X):-
	insiemeSiti(Siti),
	member(IdS,Siti),
	siti(IdS,_,_,_,_,X,_,_).

:- hilog ySitiInArea.
ySitiInArea(Y):-
	insiemeSiti(Siti),
	member(IdS,Siti),
	siti(IdS,_,_,_,_,_,Y,_).

